/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converter;

import java.net.URI;
import jpa.entities.UsuarioCli;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;
import jpa.entities.Usuario;
import jpa.entities.Endereco;
import java.util.Collection;

/**
 *
 * @author carlos
 */

@XmlRootElement(name = "usuarioCli")
public class UsuarioCliConverter {
    private UsuarioCli entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of UsuarioCliConverter */
    public UsuarioCliConverter() {
        entity = new UsuarioCli();
    }

    /**
     * Creates a new instance of UsuarioCliConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public UsuarioCliConverter(UsuarioCli entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getIdCli() + "/").build() : uri;
        this.expandLevel = expandLevel;
        getUsuario();
        getEnderecoCollection();
    }

    /**
     * Creates a new instance of UsuarioCliConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public UsuarioCliConverter(UsuarioCli entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for idCli.
     *
     * @return value for idCli
     */
    @XmlElement
    public Integer getIdCli() {
        return (expandLevel > 0) ? entity.getIdCli() : null;
    }

    /**
     * Setter for idCli.
     *
     * @param value the value to set
     */
    public void setIdCli(Integer value) {
        entity.setIdCli(value);
    }

    /**
     * Getter for cpf.
     *
     * @return value for cpf
     */
    @XmlElement
    public String getCpf() {
        return (expandLevel > 0) ? entity.getCpf() : null;
    }

    /**
     * Setter for cpf.
     *
     * @param value the value to set
     */
    public void setCpf(String value) {
        entity.setCpf(value);
    }

    /**
     * Getter for usuario.
     *
     * @return value for usuario
     */
    @XmlElement
    public UsuarioConverter getUsuario() {
        if (expandLevel > 0) {
            if (entity.getUsuario() != null) {
                return new UsuarioConverter(entity.getUsuario(), uri.resolve("usuario/"), expandLevel - 1, false);
            }
        }
        return null;
    }

    /**
     * Setter for usuario.
     *
     * @param value the value to set
     */
    public void setUsuario(UsuarioConverter value) {
        entity.setUsuario((value != null) ? value.getEntity() : null);
    }

    /**
     * Getter for enderecoCollection.
     *
     * @return value for enderecoCollection
     */
    @XmlElement
    public EnderecoesConverter getEnderecoCollection() {
        if (expandLevel > 0) {
            if (entity.getEnderecoCollection() != null) {
                return new EnderecoesConverter(entity.getEnderecoCollection(), uri.resolve("enderecoCollection/"), expandLevel - 1);
            }
        }
        return null;
    }

    /**
     * Setter for enderecoCollection.
     *
     * @param value the value to set
     */
    public void setEnderecoCollection(EnderecoesConverter value) {
        entity.setEnderecoCollection((value != null) ? value.getEntities() : null);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the UsuarioCli entity.
     *
     * @return an entity
     */
    @XmlTransient
    public UsuarioCli getEntity() {
        if (entity.getIdCli() == null) {
            UsuarioCliConverter converter = UriResolver.getInstance().resolve(UsuarioCliConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved UsuarioCli entity.
     *
     * @return an resolved entity
     */
    public UsuarioCli resolveEntity(EntityManager em) {
        Usuario usuario = entity.getUsuario();
        if (usuario != null) {
            entity.setUsuario(em.getReference(Usuario.class, usuario.getIdUsuario()));
        }
        Collection<Endereco> enderecoCollection = entity.getEnderecoCollection();
        Collection<Endereco> newenderecoCollection = new java.util.ArrayList<Endereco>();
        if (enderecoCollection != null) {
            for (Endereco item : enderecoCollection) {
                newenderecoCollection.add(em.getReference(Endereco.class, item.getIdEndereco()));
            }
        }
        entity.setEnderecoCollection(newenderecoCollection);
        return entity;
    }
}
